<template>
	<view class="container">
		<view class="wrapper">
			<view class="input-content">
				<view class="cu-form-group" style="border: 2upx solid whitesmoke;margin-bottom: 20px;border-radius: 30px">
					<view class="title">用户名</view>
					<input :value="userName" placeholder="请输入用户名" data-key="userName" @input="inputChange" />
				</view>
				<view class="cu-form-group" style="border: 2upx solid whitesmoke;margin-bottom: 20px;border-radius: 30px">
					<view class="title">手机号</view>
					<input type="number" :value="phone" placeholder="请输入手机号" maxlength="11" data-key="phone" @input="inputChange" />
				</view>
				<view class="cu-form-group" style="border: 2upx solid whitesmoke;margin-bottom: 20px;border-radius: 30px">
					<text class="title">验证码</text>
					<input type="number" :value="code" placeholder="请输入验证码" maxlength="6" data-key="code" @input="inputChange"
					 @confirm="toLogin" />
					<button class="send-msg" @click="sendMsg" :disabled="sending">{{ sendTime }}</button>
				</view>
				<view class="cu-form-group" style="border: 2upx solid whitesmoke;margin-bottom: 20px;border-radius: 30px">
					<text class="title">设置密码</text>
					<input type="password" :value="password" placeholder="请设置密码" placeholder-class="input-empty" maxlength="20"
					 minlength="6" data-key="password" @input="inputChange" @confirm="toLogin" />
				</view>
				
				<view class="cu-form-group" style="border: 2upx solid whitesmoke;margin-bottom: 20px;border-radius: 30px">
					<span>性别:</span>
						<radio-group class="sexRadio" @change="sexChange" v-for="(item, index) in sex" :key="item.value">
						<radio color="#2DCF8C" :value="item.value + ''" :checked="item.label === gender" />
						<b>{{item.label}}</b>
					</radio-group>
					<!-- <view class="title">性别</view>
					<input :value="gender" placeholder="请选择性别" data-key="gender" @input="inputChange" /> -->
				</view>
				<view class="cu-form-group" style="border: 2upx solid whitesmoke;margin-bottom: 20px;border-radius: 30px">
					<view class="title">昵称</view>
					<input :value="nick" placeholder="请输入昵称" data-key="nick" @input="inputChange" />
				</view>
				<view class="cu-form-group" style="border: 2upx solid whitesmoke;margin-bottom: 20px;border-radius: 30px">
					<view class="title">个性签名</view>
					<input :value="signature" placeholder="请输入个性签名" data-key="signature" @input="inputChange" />
				</view>
				<view class="cu-form-group" style="border: 2upx solid whitesmoke;margin-bottom: 20px;border-radius: 30px">
					<view style="color: black;">生日</view>
					<view>
						<view class="cu-form-group">
							<view  @click="show = true">
								<input style="color: #FFFFFF;float: left;" v-model="birthday" disabled="" />
								<u-calendar v-model="show" mode="date" @change="change"></u-calendar>
								<image src="https://cdn.uviewui.com/uview/demo/calendar/7.png"
									style="height: 16px;width: 16px;">
							</view>
						</view>
					</view>
					<!-- <input :value="birthday" placeholder="请设置生日" data-key="birthday" @input="inputChange" /> -->
				</view>
				<view class="cu-form-group" style="border: 2upx solid whitesmoke;margin-bottom: 20px;border-radius: 30px">
					<text>{{birthday}}</text>
				</view>
			</view>
			<button class="confirm-btn" @click="toLogin">立即注册</button>
			<view class="footer">
				<text @tap="isShowAgree" class="cuIcon" :class="showAgree ? 'cuIcon-radiobox' : 'cuIcon-round'">同意</text>
				<!-- 协议地址 -->
				<navigator url="/my/setting/mimi" open-type="navigate">《隐私政策》</navigator>
				和
				<navigator url="/my/setting/xieyi" open-type="navigate">《用户服务协议》</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userName: '',
				showAgree: false,
				code: '',
				phone: '',
				password: '',
				gender:'',
				nick:'',
				signature:'',
				birthday:'',				
				required: '否',
				sending: false,
				sendTime: '获取验证码',
				count: 60,
				relation: "",
				state: '',
				invitation: '',
				platform: '',
				sex: [{
							value: 1,
							label: '男'
						},
						{
							value: 2,
							label: '女'
						}
					],
				show:false,
				mode: 'single'

			};
		},
		onLoad() {
			//this.invitation = uni.getData('relation')
			// #ifdef APP-PLUS

			this.platform = 'app'

			//#endif


			//#ifdef H5

			this.platform = 'H5'
		},
		methods: {
			change(e) {
				console.log(e);
				this.birthday = e.result;
				console.log(this.birthday);
			},
			isShowAgree() {
				//是否选择协议
				this.showAgree = !this.showAgree;
			},
			sendMsg() {
				const {
					phone
				} = this;
				if (!phone) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'error',
						duration: 1500 
					})
					//uni.showToast('请输入手机号');
				} else if (phone.length !== 11) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'error',
						duration: 1500 
					})
					//uni.showToast('请输入正确的手机号');
				} else {
					uni.showLoading({
						title: '正在发送验证码...'
					})
					this.$myRequest({
						url:'',
						method:''
					}).then(r=>{
						if (r.data.code==200) {
							this.sending = true;
							uni.showToast({
								title: '验证码发送成功请注意查收',
								icon: 'success',
								duration: 1500 
							})
							//uni.showToast('验证码发送成功请注意查收');
							this.countDown();
							uni.hideLoading();
						} else {
							uni.hideLoading();
							uni.showModal({
								showCancel: false,
								title: '短信发送失败',
								content: res.msg ? res.msg : '请一分钟后再获取验证码'
							});
						}
					})
					// this.$Request.getT('/msg/sendMsg/' + phone + '/register').then(res => {
					// 	if (res.status === 0) {
					// 		this.sending = true;
					// 		uni.showToast({
					// 			title: '验证码发送成功请注意查收',
					// 			icon: 'success',
					// 			duration: 1500 
					// 		})
					// 		//uni.showToast('验证码发送成功请注意查收');
					// 		this.countDown();
					// 		uni.hideLoading();
					// 	} else {
					// 		uni.hideLoading();
					// 		uni.showModal({
					// 			showCancel: false,
					// 			title: '短信发送失败',
					// 			content: res.msg ? res.msg : '请一分钟后再获取验证码'
					// 		});
					// 	}
					// });
				}
			},
			countDown() {
				const {
					count
				} = this;
				if (count === 1) {
					this.count = 60;
					this.sending = false;
					this.sendTime = '获取验证码';
				} else {
					this.count = count - 1;
					this.sending = true;
					this.sendTime = count - 1 + '秒后重新获取';
					setTimeout(this.countDown.bind(this), 1000);
				}
			},
			inputChange(e) {
				const key = e.currentTarget.dataset.key;
				this[key] = e.detail.value;
			},
			sexChange(evt) { //性别选择
				for (let i = 0; i < this.sex.length; i++) {
					if (this.sex[i].value == evt.target.value) {
						this.gender = this.sex[i].label; //把获取到的值赋值给 this.gender
						console.log(this.gender)
						break;
					}
				}
			},
			navBack() {
				uni.navigateBack();
			},

			navTo(url) {
				uni.navigateTo({
					url
				});
			},
			toLogin() {
				const {
					userName,
					phone,
					password,
					code,
					showAgree,
					gender,
					nick,
					signature,
					birthday,
				} = this;
				if (!userName) {
					uni.showToast({
						title: '请输入用户名',
						icon: 'error',
						duration: 1500 
					})
					//uni.showToast('请输入用户名');
				} else if (!phone) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'error',
						duration: 1500 
					})
					//uni.showToast('请输入手机号');
				} else if (!password) {
					uni.showToast({
						title: '请设置密码',
						icon: 'error',
						duration: 1500 
					})
					//uni.showToast('请设置密码');
				} else if (password.length < 6) {
					uni.showToast({
						title: '密码位数必须大于六位',
						icon: 'error',
						duration: 1500 
					})
					//uni.showToast('密码位数必须大于六位');
				} else if (!showAgree) {
					uni.showToast({
						title: '请先同意《协议》',
						icon: 'error',
						duration: 1500 
					})
					//uni.showToast('请先同意《协议》');
				} 
				// else if (!invitation && this.required == '否') {
				// 	this.invitation = '';
				// 	this.logining = true;
				// 	uni.showLoading('注册中...');
				// 	this.$Request
				// 		.postJson(`/user/register?msg=${code}`, {
				// 			userName: userName,
				// 			password: password,
				// 			phone: phone,
				// 			openId: uni.getData('openid'),
				// 			invitation: this.invitation,
				// 			platform: this.platform
				// 		})
				// 		.then(res => {
				// 			if (res.status === 0) {
				// 				uni.setData("token", res.data.uuid);
				// 				uni.setData('userId', res.data.userId);
				// 				uni.setData('userName', res.data.userName);
				// 				uni.setData('phone', res.data.phone);
				// 				uni.setData('headImg', res.data.headImg);
				// 				this.getUserInfo(res.data.userId)
				// 				uni.navigateTo({
				// 					url: '/pages/index/index'
				// 				});
				// 			} else {
				// 				uni.hideLoading();
				// 				uni.showModal({
				// 					showCancel: false,
				// 					title: '注册失败',
				// 					content: res.msg
				// 				});
				// 			}
				// 		});
				// } 
				else {
					this.logining = true;
					uni.showLoading({
						title: '注册中...',
						//icon: 'loading'
						//duration: 1500 
					})
					//uni.showLoading('注册中...');
					this.$myRequest({
						url:"/user/register",
						method:"post",
						data:{
							account: userName,
							password: password,
							phone: phone,
							gender:gender,
							nick:nick,
							signature:signature,
							birthday:birthday,
						}
					}).then(r=>{
						console.log(r)
						if(r.data.code==200){
							uni.navigateTo({
								url: '/pages/public/login'
							})
						}
						// if (r.status === 0) {
						// 	uni.setData("token", r.data.uuid);
						// 	uni.setData('userId', r.data.userId);
						// 	uni.setData('userName', r.data.userName);
						// 	uni.setData('phone', r.data.phone);
						// 	uni.setData('headImg', r.data.headImg);
						// 	this.getUserInfo(r.data.userId)
						// 	uni.navigateTo({
						// 		url: '/pages/index/index'
						// 	});
						// } else {
						// 	uni.hideLoading();
						// 	uni.showModal({
						// 		showCancel: false,
						// 		title: '注册失败',
						// 		content: r.msg
						// 	});
						// }
					})
				}
			},
			//获取用户信息
			getUserInfo(userId) {
				this.$Request.getT('/user/find?id=' + userId).then(res => {
					if (res.status === 0) {
						uni.setData('headImg', res.data.headImg);
						uni.setData('userId', res.data.userId);
						uni.setData('userName', res.data.userName);
						uni.setData('phone', res.data.phone);
						this.phone = res.data.phone;
						this.headImg = res.data.headImg;
						this.userName = res.data.userName;
						if (this.userName == null) {
							this.userName = res.data.nickName;
						} else {
							this.userName = res.data.userName;
						}
						uni.hideLoading();
						uni.switchTab({
							url: '/pages/index/index'
						});
					} else {
						uni.showModal({
							showCancel: false,
							title: '登录失败',
							content: res.msg
						});
						uni.logout();
					}
				});
			}
		}
	};
</script>

<style lang="scss">


	.footer {
		padding-left: 140upx;
		margin-top: 32upx;
		text-align: center;
		display: flex;
		color: #FFFFFF;
	}

	.send-msg {
		border-radius: 30px;
		color: white;
		height: 30px;
		font-size: 14px;
		line-height: 30px;
		background: #5E81F9;
	}


	.container {
		padding-top: 32upx;
		position: relative;
		width: 100%;
		height: 100%;
		overflow: hidden;
		background: #111224;
	}

	.wrapper {
		position: relative;
		z-index: 90;
		background: #111224;
		padding-bottom: 20px;
	}


	.input-content {
		padding: 32upx 80upx;
	}


	.confirm-btn {
		width: 600upx;
		height: 80upx;
		line-height: 80upx;
		border-radius: 60upx;
		margin-top: 32upx;
		background: #5E81F9;

		color: #fff;
		font-size: 32upx;

		&:after {
			border-radius: 60px;
		}
	}

</style>
